package org.colcon.web.client.ui.panel;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.client.ui.ItemPanel;
import org.colcon.web.client.ui.LinkPanel;
import org.colcon.web.model.Title;
import org.colcon.web.model.framework.IItem;
import org.colcon.web.model.framework.ILink;
import org.colcon.web.model.framework.IUser;
import org.colcon.web.model.parsers.CLIParser;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

public class UserPanel extends ItemPanel 
{
	public UserPanel(Collective_Congress client, IUser user) 
	{
		super(client, user);
		
		final String path = user.titles();
		
		getService().open(path, new AsyncCallback<String[]>()
		{
			public void onFailure(Throwable caught)
			{
				add(new Label("failed to open container '" + path + "'"));
			}
			
			public void onSuccess(String[] array)
			{
				for(String str : array)
				{
					IItem item = CLIParser.parseItem(str);
						
					if(item == null || !(item instanceof ILink))
						continue;
					
					String link = ((ILink)item).getLink();
					
					add(new LinkPanel(getClient(), "link-path", Title.pathToLongId(link), link));
				}
			}
		});
		
		/* diplay titles with LinkContainerPanel

		getService().get(path, new AsyncCallback<String>()
		{
			public void onFailure(Throwable caught)
			{
				add(new Label("failed to open '" + path + "'"));
			}
			
			public void onSuccess(String str)
			{
				IItem item = CLIParser.parseItem(str);
						
				if(item == null || !(item instanceof ILinkContainer))
				{
					add(new Label("failed to open '" + path + "'"));
					return;
				}
				
				add(new LinkContainerPanel(getClient(), (ILinkContainer) item));
			}
		});
		
		*/
	}

	public IUser getUser()
	{
		return (IUser) getItem();
	}
}
